package com.system.cacheCenterBackend.service.cacheService;

import com.system.cacheCenterBackend.service.ZSetCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * Redis ZSet类型缓存服务实现
 */
@Service
public class RedisZSetCacheServiceImpl implements ZSetCacheService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Boolean zsetAdd(String key, String value, double score) {
        try {
            return stringRedisTemplate.opsForZSet().add(key, value, score);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Long zsetRemove(String key, String value) {
        try {
            return stringRedisTemplate.opsForZSet().remove(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    @Override
    public Set<String> zsetRange(String key, long start, long end) {
        try {
            return stringRedisTemplate.opsForZSet().range(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Set<String> zsetRangeByScore(String key, double min, double max) {
        try {
            return stringRedisTemplate.opsForZSet().rangeByScore(key, min, max);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Double zsetScore(String key, String value) {
        try {
            return stringRedisTemplate.opsForZSet().score(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Long zsetSize(String key) {
        try {
            return stringRedisTemplate.opsForZSet().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }
}